package com.syp.hehe.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.syp.hehe.dao.FavoriteMapper;
import com.syp.hehe.domain.RecordStatusEnum;
import com.syp.hehe.domain.pojo.FavoriteSchema;
import com.syp.hehe.domain.vo.FavoriteVo;
import com.syp.hehe.service.FavoriteServiceI;
import com.syp.hehe.util.paging.Page;
import com.syp.hehe.util.paging.PageOffSet;

@Service("favoriteService")
public class FavoriteServiceImpl implements FavoriteServiceI {
	
	private static final Logger logger = LoggerFactory.getLogger(FavoriteServiceImpl.class);
	
	@Autowired
	private FavoriteMapper favoriteMapper;

	@Override
	public void add(FavoriteVo favorite) {
		FavoriteSchema favoriteSchema = new FavoriteSchema();

		favoriteSchema.setUuid(favorite.getUuid());
		favoriteSchema.setArticleId(favorite.getArticleId());
		favoriteSchema.setUsername(favorite.getUsername());
		favoriteSchema.setStatus(RecordStatusEnum.NORMAL.getStatus());
		favoriteSchema.setCreateTime(Calendar.getInstance().getTime());

		try{
			this.favoriteMapper.insert(favoriteSchema);
		} catch (DuplicateKeyException e) {
			logger.error(String.format("主键冲突，您{%s}已经收藏了该文章{%d}。", favorite.getUsername(), favorite.getArticleId()));
		}
	}

	@Override
	public void delete(String username, Long articleId) {
		this.favoriteMapper.deleteByUnameAndArtid(username, articleId);		
	}

	@Override
	public Boolean favoriteByUser(String username, Long articleId) {
		Long count = this.favoriteMapper.countByUnameAndArtid(username, articleId);
		return count > 0L;
	}

	@Override
	public FavoriteVo getByUUID(String uuid) {
		FavoriteSchema favoriteSchema = this.favoriteMapper.getByUUID(uuid);
		
		FavoriteVo favorite = new FavoriteVo();
		favorite.setId(favoriteSchema.getId());
		favorite.setUuid(favoriteSchema.getUuid());
		favorite.setArticleId(favoriteSchema.getArticleId());
		favorite.setUsername(favoriteSchema.getUsername());
		favorite.setCreateTime(favoriteSchema.getCreateTime());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		favorite.setCreateTimeText(df.format(favoriteSchema.getCreateTime()));
		
		return favorite;
	}

	@Override
	public FavoriteVo getByUnameAndArtid(String username, Long articleId) {
		FavoriteSchema favoriteSchema = this.favoriteMapper.getByUnameAndArtid(username, articleId);
		
		FavoriteVo favorite = new FavoriteVo();
		favorite.setId(favoriteSchema.getId());
		favorite.setUuid(favoriteSchema.getUuid());
		favorite.setArticleId(favoriteSchema.getArticleId());
		favorite.setUsername(favoriteSchema.getUsername());
		favorite.setCreateTime(favoriteSchema.getCreateTime());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		favorite.setCreateTimeText(df.format(favoriteSchema.getCreateTime()));
		
		return favorite;
	}

	@Override
	public Page<FavoriteVo> getPageList(Long articleId, Long pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageOffSet<FavoriteVo> getPageOffSetList(Long articleId, Long referId, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

}
